# -*- coding: utf-8 -*-

import logging
import os
from logging.handlers import RotatingFileHandler
from tornado.log import enable_pretty_logging
from apps import configs

LOG_SIZE = 1048576
LOG_NAME = os.path.join(configs.BASE_DIR, 'logs', 'apps.log')
LOG_FILES = 100


class Logger(object):
    is_inited = False

    @staticmethod
    def init():
        if not configs.LOGGER_LEVEL:
            configs.LOGGER_LEVEL = logging.NOTSET
        rt = RotatingFileHandler(filename=LOG_NAME, maxBytes=LOG_SIZE, backupCount=LOG_FILES, encoding='utf-8')
        rt.setLevel(configs.LOGGER_LEVEL)
        rt.setFormatter(logging.Formatter('[%(asctime)s] [%(levelname)s] %(name)s [%(filename)s - %(funcName)s - line %(lineno)s]: %(message)s'))
        log = logging.getLogger()
        enable_pretty_logging()
        log.addHandler(rt)
        Logger.is_inited = True